<?php
include "class.phpmailer.php";
include "class.smtp.php";
class DataProvider
{
	public static function ExecuteQuery($sql)
	{
		$connection = mysql_connect("localhost","root","") or
		die ("couldn't connect to localhost");
		// ebookDB : Tên CSDL
		mysql_select_db("gomsu",$connection);
		mysql_query("set names 'utf8'");
		$result = mysql_query($sql,$connection);
		mysql_close($connection);
		return $result;
	}
	public static function loaisp_Trung($loaimoi)
	{

		$sql="select DISTINCT tenloaisanpham from loaisanpham";
		$result=DataProvider::ExecuteQuery($sql);
		while($row=mysql_fetch_array($result))
		{

			if(strcasecmp($loaimoi,$row["tenloaisanpham"])==0)
			{
				return 1;
			}
		}
		return 0;
	}
	public static function them_loaisanpham($submit,$ten,$alias)
	{
		if(isset($submit) && $submit=="Thêm")
		{
			if(DataProvider::loaisp_Trung($ten)==0)
			{
				$sql="INSERT INTO loaisanpham(`tenloaisanpham`,`data-filter`) values('".$ten."','".$alias."')";
				DataProvider::ExecuteQuery($sql);
			}
		}
	}
	public static function ktdangnhap($username,$password)
	{
		$u=$username;
		$p=$password;
		$sql="select * from users where username='$u' and password='$p'";
		$result=DataProvider::ExecuteQuery($sql);
		if (mysql_num_rows($result)>=1) {
			return 1;
		}
		else{
			return 0;
		}
	}
	public static function islogin($SESSION)
	{
		if((isset($SESSION["islogin"]) && $SESSION["islogin"]==true))
			return 1;
		return 0;
	}
	public static function ktquyenhan($username,$password)
	{
		if (islogin($_SESSION)==0) {
			return 0;
		}
			$u=mysql_real_escape_string($username);
			$p=mysql_real_escape_string($password);
			$sql="select quyenhan from users where username='$u' and password='$p'";
			$result=DataProvider::ExecuteQuery($sql);
			if (mysql_num_rows($result)>=1)
			{
				$row=mysql_fetch_array($result);
				if ($row["quyenhan"]==1)
					return 1;
				else
					return -1;
			}
			else
				return 0;
	}
	public static function docfile($filename)
	{
		$fp=file_get_contents($filename,FILE_TEXT) or exit("khong tim thay file can mo");
		utf8_decode($fp);
		$fp=explode(";", $fp);
		return $fp;
	}
	public static function ghifile($filename,$str)
	{
		$fi=fopen($filename, 'w');
	 	$str2=$str["tencongty"].";".$str["diachi"].";".$str["dienthoai"].";".$str["email"].";".$str["yahoo"];
	 	fwrite($fi, $str2);
	 	fclose($fi);
 	}
 	public static function doimatkhau($u,$op,$np)
	{
		$sql="update users set password='$np' where username='$u' and password='$op'";
		$kq=DataProvider::ExecuteQuery($sql);
		if ($kq)
			return 1;
		return 0;
 	}
	public static function layTatCaLoaiSanPham()
	{
		$sql="select * from loaisanpham";
		return $result=DataProvider::ExecuteQuery($sql);
	}
	public static function laySanPhamTheoLoaiSanPham($id)
	{
		$sql="select * from sanpham where id_danhmuc=$id LIMIT 0,10";
		return $result=DataProvider::ExecuteQuery($sql);
	}
	public static function layTatCaTieuDeTinTuc()
	{
		$sql="select * from tintuc";
		return $result=DataProvider::ExecuteQuery($sql);
	}

}
?>
<?php
 function SendMail($username,$password,$from_,$to_,$fromname_,$name_,$tieude,$noidung)
 {
	 $mail = new PHPMailer();
	 $mail->
IsSMTP();
	 $mail->Host = "smtp.gmail.com";
	 $mail->Port = 465;
	 $mail->SMTPAuth = true;
	 $mail->SMTPAuth = true;
	 $mail->SMTPSecure = 'ssl';
	 $mail->Username = $username; // your SMTP username or your gmail username
	 $mail->Password = $password; // your SMTP password or your gmail password
	$mail->From = $from_;
	$mail->FromName = $fromname_; // Name to indicate where the email came from when the recepient received
	$mail->AddAddress($to_,$name_);
	//$mail->AddReplyTo($from,"Vo Duy Tuan");
	$mail->WordWrap = 50; // set word wrap
	$mail->IsHTML(true); // send as HTML
	$mail->Subject = $tieude;
	$mail->Body = $noidung; //HTML Body
	$mail->AltBody = "Mail nay duoc goi bang phpmailer class"; //Text Body
	if(!$mail->Send())
	{
		echo "
<h1>Loi khi goi mail: " . $mail->ErrorInfo . '</h1>
';
		return 0;
	}
	else
	{
		return 1;
	}


}
?>